import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
import { LotteryService } from './lottery.service';
import { LotteryConfig, LotteryItem } from './lottery-config.entity';

// 定义创建配置的请求参数类型（明确排除奖项的id）
type CreateLotteryDto = {
  // 抽奖时长（可选，默认10秒）
  drawDuration?: number;
  // 是否显示配置（可选，默认true）
  isShow?: boolean;
  // 背景图URL（可选）
  bgImage?: string;
  // 其他图片URL（可选）
  otherImage?: string;
  // 初始奖项列表（可选，每个奖项不包含id）
  items?: Omit<LotteryItem, 'id'>[];
};


@Controller('lottery')
export class LotteryController {
  constructor(private readonly lotteryService: LotteryService) { }

  /**
   * 创建抽奖配置
   * POST /lottery
   * 示例Body: { "items": [], "drawDuration": 15 }
   */
  // @Post()
  // create(@Body() createLotteryDto: Partial<LotteryConfig>) {
  //   return this.lotteryService.create(createLotteryDto);
  // }

  /**
 * 创建抽奖配置（初始化）
 * POST /lottery
 * 前端无需为items中的奖项传id，自动生成
 */
  @Post()
  create(@Body() createDto: CreateLotteryDto) {
    // Cast to Partial<LotteryConfig> so the service accepts items without ids;
    // the service will generate/overwrite item ids when creating the config.
    return this.lotteryService.create(createDto as unknown as Partial<LotteryConfig>);
  }


  /**
   * 获取所有抽奖配置
   * GET /lottery
   */
  @Get()
  findAll() {
    return this.lotteryService.findAll();
  }

  /**
   * 获取单个抽奖配置
   * GET /lottery/:id
   */
  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.lotteryService.findOne(+id);
  }

  /**
   * 整体更新抽奖配置
   * PATCH /lottery/:id
   * 示例Body: { "items": [...], "drawDuration": 20 }
   */
  @Patch(':id')
  update(
    @Param('id') id: string,
    @Body() updateLotteryDto: Partial<LotteryConfig>,
  ) {
    return this.lotteryService.update(+id, updateLotteryDto);
  }

  /**
   * 单独更新抽奖时长
   * PATCH /lottery/:id/duration
   * 示例Body: { "drawDuration": 25 }
   */
  @Patch(':id/duration')
  updateDuration(
    @Param('id') id: string,
    @Body('drawDuration') drawDuration: number,
  ) {
    return this.lotteryService.updateDuration(+id, drawDuration);
  }

  /**
   * 新增：控制配置显示/隐藏
   * PATCH /lottery/:id/show
   * 示例Body: { "isShow": false }
   */
  @Patch(':id/show')
  updateIsShow(
    @Param('id') id: string,
    @Body('isShow') isShow: boolean,
  ) {
    return this.lotteryService.updateIsShow(+id, isShow);
  }

  /**
   * 新增：单独更新背景图
   * PATCH /lottery/:id/bg-image
   * 示例Body: { "bgImage": "https://xxx.com/bg.jpg" }
   */
  @Patch(':id/bg-image')
  updateBgImage(
    @Param('id') id: string,
    @Body('bgImage') bgImage: string,
  ) {
    return this.lotteryService.updateBgImage(+id, bgImage);
  }

  /**
   * 新增：单独更新其他图片
   * PATCH /lottery/:id/other-image
   * 示例Body: { "otherImage": "https://xxx.com/other.jpg" }
   */
  @Patch(':id/other-image')
  updateOtherImage(
    @Param('id') id: string,
    @Body('otherImage') otherImage: string,
  ) {
    return this.lotteryService.updateOtherImage(+id, otherImage);
  }

  /**
   * 单条添加奖项
   * POST /lottery/:configId/items
   * 示例Body: { "id": 1, "text": "200元", "probability": 8, "isSpecial": false }
   */
  @Post(':configId/items')
  addSingleItem(
    @Param('configId') configId: string,
    // @Body() newItem: LotteryItem,
    @Body() newItem: Omit<LotteryItem, 'id'>, // 不包含id字段
  ) {
    return this.lotteryService.addSingleItem(+configId, newItem);
  }

  /**
   * 单条更新奖项
   * PATCH /lottery/:configId/items/:itemId
   * 示例Body: { "probability": 10, "text": "200元现金" }
   */
  @Patch(':configId/items/:itemId')
  updateSingleItem(
    @Param('configId') configId: string,
    @Param('itemId') itemId: string,
    @Body() updateData: Partial<LotteryItem>,
  ) {
    return this.lotteryService.updateSingleItem(+configId, +itemId, updateData);
  }

  /**
   * 单条删除奖项
   * DELETE /lottery/:configId/items/:itemId
   */
  @Delete(':configId/items/:itemId')
  deleteSingleItem(
    @Param('configId') configId: string,
    @Param('itemId') itemId: string,
  ) {
    return this.lotteryService.deleteSingleItem(+configId, +itemId);
  }

  /**
   * 删除整个抽奖配置
   * DELETE /lottery/:id
   */
  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.lotteryService.remove(+id);
  }
}